﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using web_search;

namespace BookSystemLyn
{
    public partial class FormAdminBorrowRec : Form
    {
        public FormAdminBorrowRec()
        {
            InitializeComponent();
        }

        void loadAllUserBorrowRec()
        {
            //首先清空表格
            dgv_allborrow.Rows.Clear();

            //连接数据库
            DaoAdmin dao=new DaoAdmin();
            dao.connect();
            string sql = $"select * from V_UserBorrowList";
            SqlDataReader reader=dao.read(sql);
            
            string bookstatus = ""; 
            while(reader.Read()) {
                if (reader[8].ToString() == "0")
                {
                    bookstatus = "已归还";
                }
                else if (reader[8].ToString() == "1")
                {
                    DateTime now = DateTime.Now;
                    DateTime returntime = (DateTime)reader[6];
                    if(now > returntime)
                    {
                        bookstatus = "已逾期";
                    }
                    else
                    {
                        bookstatus = "未归还";
                    }
                }    
                dgv_allborrow.Rows.Add(
                      reader[0].ToString(),
                      reader[1].ToString(),
                      reader[2].ToString(),
                      reader[3].ToString(),
                      reader[5].ToString(),
                      reader[6].ToString(),
                      reader[7].ToString(),
                      bookstatus
                     );
            }

        }
        private void FormAdminBorrowRec_Load(object sender, EventArgs e)
        {
            //加载
            loadAllUserBorrowRec();
        }

        //点击某一列，定位
        private void dgv_allborrow_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取当前选中的行
            if (dgv_allborrow.CurrentRow == null || dgv_allborrow.CurrentRow.Cells[0].Value == null)
            {
                return;
            }

            labeluserid.Text = dgv_allborrow.CurrentRow.Cells[0].Value.ToString();
            labelbooid.Text = dgv_allborrow.CurrentRow.Cells[2].Value.ToString();
            labelbookname.Text = dgv_allborrow.CurrentRow.Cells[3].Value.ToString();   
            if(dgv_allborrow.CurrentRow.Cells[3].Value.ToString() == "已逾期")
            {
                labellate.Text = "是";
            }
            else
            {
                labellate.Text = "否";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            loadAllUserBorrowRec();
            MessageBox.Show("刷新成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //搜索
        private void button1_Click(object sender, EventArgs e)
        {
            //拿到关键字
            string keyword = textkey.Text.Trim();

            //根据关键字查询数据库
            DaoAdmin dao = new DaoAdmin();
            dao.connect();

            string sql = $"select * from V_UserBorrowList where BookID like '%{keyword}%' or BookName like '%{keyword}%' or UserID like '%{keyword}%' or UserName like '%{keyword}%'";

            SqlDataReader reader = dao.read(sql);

            //清空表格
            dgv_allborrow.Rows.Clear();

            //将查询到的数据显示到表格中
            string bookstatus = "";
            while (reader.Read())
            {
                if (reader[8].ToString() == "0")
                {
                    bookstatus = "已归还";
                }
                else if (reader[8].ToString() == "1")
                {
                    DateTime now = DateTime.Now;
                    DateTime returntime = (DateTime)reader[6];
                    if (now > returntime)
                    {
                        bookstatus = "已逾期";
                    }
                    else
                    {
                        bookstatus = "未归还";
                    }
                }
                dgv_allborrow.Rows.Add(
                      reader[0].ToString(),
                      reader[1].ToString(),
                      reader[2].ToString(),
                      reader[3].ToString(),
                      reader[5].ToString(),
                      reader[6].ToString(),
                      reader[7].ToString(),
                      bookstatus
                     );
            }

        }
    }
}
